<?php
session_name(DicomScience);
session_start();

/*
 * This is the PDF Controller File to be called to create a PDF. Use this in a popup Window since Mozilla does have an
 * issue with popup or framed PDF documents.
 */

require("./lib/fpdf/fpdf.php");

/*
 * ACTION CONTROLLER: Decide what PDF to create depending on the passed URL GET string
 * 
 * Valid strings are: 
 * ?report=[reportNumber]
 * ?case=[caseNumber]
 * 
 */ 
 
$report = $_GET['report'];
$case = $_GET['case'];

if (!$report == ""){
	createReportPDF($report);
}elseif(!$case == ""){
	createCasePDF($case);
}

// *************************************************************************************

function createReportPDF($id){
	
	require('../includes/gset.php');
	$ip_add = $_SERVER["SERVER_ADDR"];	
	/*
	 * Load Language File
	 */
	 if ($lang == '1'){
		require('../includes/language/german.php');
	} elseif ($lang == '2'){
		require('../includes/language/english.php');
	} elseif ($lang == '3'){
		require('../includes/language/french.php');
	} elseif ($lang == '4'){
		require('../includes/language/italian.php');
	}
		
	require('../includes/database/connect.php');
	
	$getReport = "SELECT id_report, headline, diagnosis, report, last_modified, status, lastname, firstname FROM reports, staff WHERE reports.id_staff = staff.id_staff AND id_report='$id';";
	$sql_getReport = mysql_query($getReport);
	
	require('../includes/database/closedb.php');
	
	while ($rep = mysql_fetch_object($sql_getReport)){
			
		$lastname = $rep->lastname;
		$firstname = $rep->firstname;
		$id = $rep->id_report;
		$headline = $rep->headline;
		$diagnosis = $rep->diagnosis;
		$last_modified = $rep->last_modified;
		$status = $rep->status;
		$reportText = $rep->report;
			
	}
		
	$date = getReportDate($last_modified);

	class myPDF extends FPDF{
		function Header(){
		}
	
		function Footer(){
	     	$this->SetY(-10); 
	        $this->SetFont('Arial','I',8); 
			$this->Cell(0,10,'Seite '.$this->PageNo().'/{nb}',0,0,'C');
			// $this->SetY(-12); 
			// $this->Cell(0,10,'DicomScience Bericht Nr. '.$id.' von '.$firstname.' '.$lastname,0,0,'C');
		}
	}
	
	$pdf = new myPDF();
	$pdf->AliasNbPages();
	$pdf->AddPage();
	$pdf->SetFont('Times', 'b', 25);
	$pdf->Cell(0,20,'DicomScience',0,1,'C');
	$pdf->SetFont('Times', 'b', 11);
	$pdf->Cell(0,8,$nls_reportHead.$id,0,1,'L');
	$pdf->Cell(0,8,$nls_headline.': '.$headline,0,1,'L');
	$pdf->Cell(0,8,$nls_diagnosis.': '.$diagnosis,0,1,'L');
	// $pdf->Cell(0,8,$nls_lastModified.': '.$date,0,1,'L');
	$pdf->Cell(0,8,$nls_mediaEditor.': '.$firstname.' '.$lastname,0,1,'L');
	$pdf->Cell(0,8,' ',0,1,'L');
	$pdf->SetFont('Times', '', 11);
	$pdf->MultiCell(0,8,$reportText,0,'L');
	$pdf->Output();	
		
}

function createCasePDF($id){
	
	require('../includes/gset.php');
	$ip_add = $_SERVER["SERVER_ADDR"];
		
	/*
	 * Load Language File
	 */
	 if ($lang == '1'){
		require('../includes/language/german.php');
	} elseif ($lang == '2'){
		require('../includes/language/english.php');
	} elseif ($lang == '3'){
		require('../includes/language/french.php');
	} elseif ($lang == '4'){
		require('../includes/language/italian.php');
	}
		
	require('../includes/database/connect.php');
	
	$getCase = "SELECT id_case, topic, last_modified, status, lastname, firstname FROM cases, staff WHERE cases.editor = staff.id_staff AND id_case='$id';";
	$sql_getCase = mysql_query($getCase);
	
	require('../includes/database/closedb.php');
	
	while ($cs = mysql_fetch_object($sql_getCase)){
			
		$lastname = $cs->lastname;
		$firstname = $cs->firstname;
		$id = $cs->id_case;
		$topic = $cs->topic;
		$last_modified = $cs->last_modified;
		$status = $cs->status;
			
	}
		
	$date = getReportDate($last_modified);

	class myPDF extends FPDF{
		function Header(){
		}
	
		function Footer(){
	     	$this->SetY(-10); 
	        $this->SetFont('Arial','I',8); 
			$this->Cell(0,10,'Seite '.$this->PageNo().'/{nb}',0,0,'C');
		}
	}
	
	$pdf = new myPDF();
	$pdf->SetCreator('DicomScience.org APP Server - '.$institute, true);
	$pdf->SetAuthor($firstname.' '.$lastname, true);
	$pdf->SetTitle($topic ,true);
	$pdf->AliasNbPages();
	$pdf->AddPage();
	$pdf->SetFont('Times', 'b', 25);
	$pdf->Cell(0,20,'DicomScience',0,1,'C');
	$pdf->SetFont('Times', 'b', 11);
	$pdf->Cell(0,8,$nls_caseHead.$id,0,1,'L');
	$pdf->Cell(0,8,$nls_headline.': '.$topic,0,1,'L');
	$pdf->Cell(0,8,$nls_mediaEditor.': '.$firstname.' '.$lastname,0,1,'L');
	$pdf->Cell(0,8,' ',0,1,'L');
	$pdf->SetFont('Times', '', 11);
	
	require('../includes/database/connect.php');
	$getCaseReport = "SELECT id_crep, reports.id_report, headline, diagnosis, report, lastname, firstname FROM reports, creps, cases, staff WHERE creps.id_case = cases.id_case	AND creps.id_report = reports.id_report	AND reports.id_staff = staff.id_staff AND creps.id_case='$id';";
	$sql_getCaseReport = mysql_query($getCaseReport);
	require('../includes/database/closedb.php');
	
	while ($cr = mysql_fetch_object($sql_getCaseReport)){
		
		$Rid = $cr->id_report;
		$Rheadline = $cr->headline;
		$Rdiagnosis = $cr->diagnosis;
		$RreportText = $cr->report;
		$Rlastname = $cr->lastname;
		$Rfirstname =$cr->firstname;
		
	}
	
	// Append a report only to the PDF if a report has been appended to the case
	if ($Rid > 0){
		$pdf->setSubject($Rdiagnosis,true);
		$pdf->SetFont('Times', 'b', 11);
		$pdf->SetX(20);
		$pdf->Cell(0,8,$nls_caseReport.$nls_from.$Rlastname." ".$Rfirstname,0,1,'L');	
		$pdf->SetFont('Times', '', 11);
		$pdf->SetX(20);
		$pdf->Cell(0,8,$nls_headline.": ".$Rheadline,0,1,'L');
		$pdf->SetX(20);
		$pdf->Cell(0,8,$nls_diagnosis.": ".$Rdiagnosis,0,1,'L');
		$pdf->SetX(20);
		$pdf->SetFont('Times', '', 10);
		$pdf->MultiCell(0,5,$RreportText,0,'J');
		
	}
	
	// Append media key images where media_type != image
	require('../includes/database/connect.php');
	$getCaseMedia = "SELECT id_c_media, c_media.media_type, location_thumb, location_media, imgrep, medrep FROM c_media, media, cases, staff WHERE c_media.id_case = cases.id_case AND c_media.id_media = media.id_media AND media.id_staff = staff.id_staff AND c_media.id_case='$id'";
	$sql_getCaseMedia = mysql_query($getCaseMedia);
	require('../includes/database/closedb.php');
	
	$pdf->SetFont('Times', 'b', 10);
	$pdf->Cell(0,8,' ',0,1,'L');
	$pdf->SetX(20);
	$pdf->Cell(0,8,$nls_imageHead,0,1,'L');
	
	$i = 0;
	
	while ($cm = mysql_fetch_object($sql_getCaseMedia)){
		if ($i == 0){ $cy = $pdf->GetY(); }	
		$i++;
		$Mthumb = $cm->location_thumb;
		$Mmedia = $cm->location_media;
		$MthumbP = $cm->imgrep;
		$MmediaP = $xm->medrep;
	
		$cx = 20 * $i;
		if ($i == 9){
			// do carriage return
			$cx = 20;
			$cy = ($pdf->GetY()+25);
			$pdf->SetY($cy);
			$i = 1;	
		}
		$pdf->Image('http://'.$ip_add.$Mthumb, $cx ,$cy ,'' ,'20' ,'' , 'http://'.$ip_add.$Mmedia);	
	}
	
	$pdf->SetY($pdf->GetY()+25);
	$pdf->Cell(0,8,' ',0,1,'L');
	$pdf->SetX(20);
	
	// Append images in full resolution where media_type == image
	require('../includes/database/connect.php');
	
	$getCaseImage = "SELECT id_image, pat_pos, manufacturer, kernel, mr_coil, mr_aquisitiontype, slice_thickness, sequence_name, location_thumb, location_image, imgrep, thbrep, modality, organ, contrast, laterality, image_comment FROM image, c_media, cases WHERE cases.id_case = c_media.id_case AND c_media.media_type='8' AND cases.id_case = '$id' GROUP BY id_c_media";
	$sql_getCaseImage = mysql_query($getCaseImage);
	
	require('../includes/database/closedb.php');
	
	while($ci = mysql_fetch_object($sql_getCaseImage)){
		
		$Iimage = $ci->location_image;
		$mod = $ci->modality;
		$org = $ci->organ;
		$lat= $ci->laterality;
		$imgC = $ci->image_comment;
		$con = $ci->contrast;
		$mrA = $ci->mr_aquisitiontype;
		$seq = $ci->sequence_name;
		$pn = $ci->protocol_name;
		$ker = $ci->kernel;
		$coil = $ci->mr_coil;
		$patP = $ci->pat_pos;
		$man = $ci->manufacturer;
		
		$pdf->addpage();
		$pdf->SetX(20);
		$pdf->SetY(20);
		$pdf->Image('http://'.$ip_add.$Iimage,'20','20','170','','','http://'.$ip_add.$Iimage);
		$iSize=getimagesize('http://'.$ip_add.$Iimage);
		$pdf->SetFont('Times', '', 8);

		// echo $iSize[1] / 72;
		$pdf->SetY($iSize[1]/4.7);
		$pdf->SetX(25);

		if ($mod != ''){ $pdf->SetX(25);$pdf->Cell(0,3,'Modality: '.$mod,0,1,'L'); }
		if ($org != ''){ $pdf->SetX(25);$pdf->Cell(0,3,'Organ: '.$org,0,1,'L'); }
		if ($lat != ''){ $pdf->SetX(25);$pdf->Cell(0,3,'Laterality: '.$lat,0,1,'L'); }
		if ($imgC != ''){ $pdf->SetX(25);$pdf->Cell(0,3,'Image Comment: '.$imgC,0,1,'L'); }
		if ($con != ''){ $pdf->SetX(25);$pdf->Cell(0,3,'Contrast: '.$con,0,1,'L'); }
		if ($mrA != ''){ $pdf->SetX(25);$pdf->Cell(0,3,'MR Acquisition Type: '.$mrA,0,1,'L'); }
		if ($seq != ''){ $pdf->SetX(25);$pdf->Cell(0,3,'Sequence Name: '.$seq,0,1,'L'); }
		if ($pn != ''){ $pdf->SetX(25);$pdf->Cell(0,3,'Protocol Name: '.$pn,0,1,'L'); }
		if ($ker != ''){ $pdf->SetX(25);$pdf->Cell(0,3,'Kernel: '.$ker,0,1,'L'); }
		if ($coil != ''){ $pdf->SetX(25);$pdf->Cell(0,3,'MR Recieving Coil: '.$coil,0,1,'L'); }	
		if ($patP != ''){ $pdf->SetX(25);$pdf->Cell(0,3,'Patient Position: '.$patP,0,1,'L'); }	
		if ($man != ''){ $pdf->SetX(25);$pdf->Cell(0,3,'Manufacturer Model Name: '.$man,0,1,'L'); }
	}
	
	// Discussion Board
	$pdf->addpage();
	$pdf->setY(20);
	$pdf->setX(20);
	$pdf->SetFont('Times', 'b', 10);
	$pdf->Cell(0,8,$nls_comment_headline,0,1,'L');
	require('../includes/database/connect.php');
	$getCaseMessages = "SELECT firstname, lastname, comment FROM cases, comments, staff WHERE cases.id_case = comments.id_case AND comments.id_case='$id' AND staff.id_staff=comments.id_staff GROUP BY id_comment ORDER BY (posted) DESC ;";
	$sql_getCaseMessages = mysql_query($getCaseMessages);
	require('../includes/database/closedb.php');
	
	while($cc = mysql_fetch_object($sql_getCaseMessages)){
		
		$fn = $cc->firstname;
		$ln = $cc->lastname;
		$com = $cc->comment;
		
		$pdf->SetFont('Times', 'b', 10);
		$pdf->setX(25);
		$pdf->Cell(0,5,$fn.': '.$ln,0,1,'L');
		$pdf->SetFont('Times', '', 10);
		$pdf->setX(30);
		$pdf->MultiCell(0,3,$com,0,'J');
		
	}

	$pdf->Output();	
	
	
}

function getReportDate($blogdate) {
	
	require('../includes/gset.php');
	
	// Defining response language
	$session_language = $_SESSION['lang'];
	if ($session_language == "") {
	
		// Assuming Controller has not been triggered by a PHP Session
		if ($lang == 1)	{
			require ('../includes/language/german.php');	
		} elseif($lang == 2){
			require ('../includes/language/english.php');
		} elseif($lang == 3){
			require ('../includes/language/french.php');
		}
			
	}
	
	$aktuelle_zeit = time();
	$lessoneminute = $aktuelle_zeit - 60;
	$lessonehour = $aktuelle_zeit - 3600;
	$lessoneday = $aktuelle_zeit - 86400;
	$yesterday = $aktuelle_zeit - 172800;
	$week = $aktuelle_zeit - 604800;
	$twoweeks = $aktuelle_zeit - 1209600;
	$month = $aktuelle_zeit - 2419200;
	$twomonths = $aktuelle_zeit - 4838400;
	$halfyear = $aktuelle_zeit - 14515200;
	$year = $aktuelle_zeit - 29030400;	
					
	if ($blogdate > $lessoneminute){
	$display_blogdate = $nls_lessoneminute;
	} elseif ($blogdate > $lessonehour){
	$display_blogdate = $nls_lessonehour;
	} elseif ($blogdate > $lessoneday){
	$display_blogdate = $nls_lessoneday;
	} elseif ($blogdate > $yesterday) {
	$display_blogdate = $nls_yesterday;
	} elseif ($blogdate > $week){
	$display_blogdate = $nls_week;
	} elseif ($blogdate > $twoweeks){
	$display_blogdate = $nls_twoweeks;
	} elseif ($blogdate > $month){
	$display_blogdate = $nls_month;
	} elseif ($blogdate > $twomonths){
	$display_blogdate = $nls_twomonths;
	} elseif ($blogdate > $halfyear){
	$display_blogdate = $nls_halfyear;
	} elseif ($blogdate > $year){
	$display_blogdate = $nls_year;
	} else {
	$display_blogdate = $nls_older;
	}

	return $display_blogdate;
}
?>